package cn.xmy.demo01.controller;

import cn.xmy.demo01.pojo.GEUser;
import cn.xmy.demo01.pojo.Collection;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AddCollectionController {

    // 自动注入 CollectionService
    @Autowired
    private CollectionService collectionService;

    // 处理 POST 请求，添加收藏
    @PostMapping("/addCollection")
    public int addCollection(HttpSession session, @RequestParam String post_id) {
        GEUser geUser = (GEUser) session.getAttribute("GUser");
        if (geUser == null) {
            // 如果没有登录或 session 失效，返回 0 或相应的失败代码
            return 0;
        }

        String uid = geUser.getUid();
        Collection collection = new Collection(uid, post_id);

        int rows = 0;
        try {
            rows = collectionService.addCollectionPostByUid(collection);
        } catch (Exception e) {
            e.printStackTrace();
            // 处理异常并返回失败
            return -1;  // 返回一个代表失败的数字
        }

        return rows;  // 返回成功操作的行数
    }
}
